/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.presentationappearance;

import java.util.ArrayList;

import org.openaecfm.model.geometry.CartesianTransformationOperator2D;
import org.openaecfm.model.presentationappearance.SurfaceTexture;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class SurfaceTextureIO implements
		IEntityInstanceUtilInput<SurfaceTexture>,
		IEntityInstanceUtilOutput<SurfaceTexture> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(SurfaceTexture pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RepeatS
		sei.addParameter(new EnumerationParameter(pojo.getRepeatS()));

		// Set RepeatT
		sei.addParameter(new EnumerationParameter(pojo.getRepeatT()));

		// Set Mode
		if (pojo.getMode() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getMode(),
					StringParameter.UTF_8));
		}

		// Set TextureTransform
		if (pojo.getTextureTransform() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getTextureTransform())));
		}

		// Set Parameter
		if (pojo.getParameter() == null || pojo.getParameter().isEmpty()) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (String s : pojo.getParameter()) {
				l.add(new StringParameter(s, StringParameter.UTF_8));
			}
			sei.addParameter(l);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			SurfaceTexture pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RepeatS
		EnumerationParameter rs = (EnumerationParameter) sei.getParameter(0);
		pojo.setRepeatS(org.openaecfm.model.measure.Boolean.valueOf(rs
				.toEnumString()));

		// Set RepeatT
		EnumerationParameter rt = (EnumerationParameter) sei.getParameter(1);
		pojo.setRepeatT(org.openaecfm.model.measure.Boolean.valueOf(rt
				.toEnumString()));

		// Set Mode
		Parameter mode = sei.getParameter(2);
		if (mode.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) mode;
			pojo.setMode(s.toUTF8());
		}

		// Set Parameter
		Parameter para = sei.getParameter(4);
		if (para.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) para;
			if (pojo.getParameter() == null) {
				pojo.setParameter(new ArrayList<String>());
			}
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				pojo.getParameter().add(s.toUTF8());
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			SurfaceTexture pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TextureTransform
		Parameter transform = sei.getParameter(3);
		if (transform.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) transform;
			pojo.setTextureTransform((CartesianTransformationOperator2D) appDataManager
					.getObject(n.toInstanceName()));
		}
	}
}
